/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.confiteriofuentes.modelo;

import com.confiteriafuentes.modelo.solicitud.Usuario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author MAN
 */
public class Usuario_Dao implements IDAO<Usuario> {

      private Model model;

        public Usuario_Dao() throws SQLException {
        model = Model.getInstance();
    }


     

    public boolean existe(Usuario o) throws SQLException{
             
             Connection conex=model.getCon();
		boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT COUNT(*) FROM usuario WHERE nombre=? AND password=?");
			pstmt.setString(1, o.getNombreUsuario());
                        pstmt.setString(2, o.getPassword());
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return sw;
	}



      public Usuario load(ResultSet rs) throws SQLException {

        Usuario u = new Usuario();
        u.setNombreUsuario(rs.getString(2));
        u.setPassword(rs.getString(3));
        u.setTipo(rs.getString(4));
        u.setCodigo(rs.getInt(5));


        return u;
    }


      public Object extrae (Integer id) throws SQLException{
        
                    Connection conex=model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
                Usuario u= new Usuario();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM Usuario WHERE idUsuario=?");
			pstmt.setInt(1,id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next()){
			  u=(Usuario)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return u;
     }

    public void insert(Usuario t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(Usuario t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Usuario> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Usuario find(Integer id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Usuario t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
      

       

}
